<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<title>我的</title>
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
	<link href="../../../css/mui.css" rel="stylesheet"/>
	<link href="../../../css/app.css" rel="stylesheet"/>
	<style type="text/css">
		#body {
			margin: 20% auto;
			width: 80%;
			text-align: center;
		}
		#signDiv {
			margin: 0 auto;
			padding-top: 80px;
			width: 200px; height: 200px;
			background: #ADD8E6;
			border: 5px solid #B0E0E6;
			border-radius: 200px;
			font-size: 16px;
			color: #4169E1;
			/* line-height: 200px; */
			box-shadow:0px 0px 20px #CCCCCC;
		}
		.small {
			margin-top: 100px;
		}
	</style>
</head>
<body>
	<!-- 头部TITLE -->
	<header class="mui-bar mui-bar-nav">
		<span class="mui-icon mui-icon-back" 
			onclick="jumpToMy()"></span>
		<h1 class="mui-title">
			<b>签到</b>
			&nbsp; 
			<span onclick="jumpToMySignList()">历史</span>
		</h1>
	</header>
	<!-- 头部TITLE -->
	
	<!-- 主体部分 -->
	<div style="height: 100px;"></div>
	<div id="body">
		<div id="signDiv" onclick="setSign()"></div>
		<p class="small">
			每小时可以签到一次
			<br>
			获取随机奖励
		</p>
	</div>
	<!-- 主体部分 -->
	
	<!-- 底部菜单 -->
	<nav class="mui-bar mui-bar-tab">
		<a class="mui-tab-item" onclick="jumpToIndex()">
			<span class="mui-icon mui-icon-home"></span>
			<span class="mui-tab-label">首页</span>
		</a>
		<a class="mui-tab-item" onclick="jumpToMarket()">
			<span class="mui-icon mui-icon-download"></span>
			<span class="mui-tab-label">库</span>
		</a>
<!-- 		<a class="mui-tab-item" onclick="jumpToMarket()">
			<span class="mui-icon mui-icon-download"></span>
			<span class="mui-tab-label">广场</span>
		</a> -->
<!-- 		<a class="mui-tab-item" onclick="jumpToCamera()">
			<span class="mui-icon mui-icon-camera"></span>
			<span class="mui-tab-label">拍</span>
		</a> -->
<!-- 		<a class="mui-tab-item" onclick="jumpToChat()">
			<span class="mui-icon mui-icon-chatbubble"></span>
			<span class="mui-tab-label">消息</span>
		</a> -->
		<a class="mui-tab-item mui-active" onclick="jumpToMy()">
			<span class="mui-icon mui-icon-contact"></span>
			<span class="mui-tab-label">我的</span>
		</a>
	</nav>
</body>
</html>





<script src="../../../js/mui.js"></script>
<script src="../../../js/app.js"></script>
<script type="text/javascript">
	// // 初始化
	// mui.plusReady(function () {
	// 	mui.init({
	// 		//下拉、上拉刷新
	// 		pullRefresh: {
	// 			container: "#refresh",
	// 			down: { //下拉刷新
	// 				style: "circle",
	// 				height: 50,
	// 				auto: true,
	// 				callback: async function () { 
	// 					jumpToMySignAdd();
	// 				}
	// 			}
	// 		}
	// 	});
	// });
	// 自动获取
	mui.plusReady(function () {
		(function($, owner) {
			getSignOne();
		}(mui, window.app = {}));
	})
	// 获取签到
	function getSignOne(){
		// 获取小时
		var hcurr = getHourCurr();
		var resList = getSignCache();
		var list = resList.list;
		var hour = 0;
		for (var i=0;i<list.length;i++) {
			if (list[i].signHour==hcurr.hour) { 
				hour = list[i].signHour; 
			}
		}
		// mui.alert('hour'+hour); return
		var h = "", hnext = 0;
		if (hour) {
			hnext = hour==12 ? "次日" : hour*1+1;
			h = hour+"点已签到<br>"+hnext+"点再签到";
			mui("#signDiv")[0].innerHTML = h;
		} else {
			h = hcurr.hour+"点可以签到";
			mui("#signDiv")[0].innerHTML = h;
		}
	}
	// 定位测试
	function setSign(){
		// mui.toast('签到待开发'); return
		mui.plusReady(function() {
			plus.geolocation.getCurrentPosition(
				translatePoint,
				function(e){
					mui.toast("异常:" + e.message);
		        }
			);
		});
	}
	// 获取定位坐标
	function translatePoint(position){
	    var currentLon = position.coords.longitude;
	    var currentLat = position.coords.latitude;
	    // mui.toast(currentLon, currentLat);
		// 书写自己的逻辑
		setUserSign(currentLon, currentLat);
	}
	// 同步用户坐标
	function setUserSign(lon, lat){
		// 系统参数
		var sys = getSys();
		var domain = sys.domain + '/app/sign/add';
		// 用户信息
		var userInfo = cacheGet(userInfoKey);
		var uid = userInfo ? userInfo.uid : 0;
		mui.ajax(domain,{
			data:{
				appAccess: sys.appAccess,
				uid: uid,
				lon: lon,
				lat: lat
			},
			dataType:'json', //服务器返回json格式数据
			type:'post', //HTTP请求类型
			timeout:10000, //超时时间设置为10秒
			headers:{
				'Content-Type':'application/x-www-form-urlencoded',
			}, 
			success:function(res){
				var hour = getHourCurr();
				if (res.code==-1) {
					mui.toast('需要更新系统参数',{ 
						duration:'short', type:'div',
					});
					setTimeout(function(){
						setSys();
						jumpToIndex();
					},500);
				} else if (res.code!=0) {
					mui.toast(hour.hour+'点已签到');
					// mui.toast(hour.hour+'点已签到或'+res.msg,{ 
					// 	duration:'short', type:'div',
					// });
					return
				}
				mui.toast('签到成功',{ 
					duration:'short', type:'div',
				});
				var signId = res.data.lastId;
				var signDate = res.data.createTime;
				var one = {
					signId:signId,
					signLon:lon,
					signLat:lat,
					signYmdh:hour.ymdh,
					signDate:signDate,
					signHour:hour.hour
				};
				// 更新缓存
				setSignCache(one);
				// 刷新页面
				setTimeout(function(){
					jumpToMySignAdd();
				}, 1000);
			},
			error:function(xhr,type,errorThrown){
				mui.toast('网络错误！',{ 
					duration:'short', type:'div',
				});
			}
		});
	}
	// 获取当前小时
	function getHourCurr(){
		var day = new Date();
		var d = day.getFullYear().toString() + (day.getMonth()+1).toString() + day.getDate().toString();
		var y = day.getFullYear();
		var m = day.getMonth()+1;
		var d = day.getDate();
		var h = day.getHours();
		if (m < 10) { m = '0'+m; }
		if (h < 10) { h = '0'+h; }
		var ymdh = y + m + d + h;
		return {
			ymdh:ymdh,
			hour:h
		};
	}
	// 获取数据缓存
	function getSignCache(){
		var resList = cacheGet(signList);
		if (!resList) {
			var list = new Array(), //数据数组
				idstring = ''; //ID字符串集合
			return {
				// 数据数组
				list: list,
				// 历史ID集合
				idstr: idstring
			};
		}
		return resList;
	}
	/** 
	 * 设置数据缓存
	 * one 新数据一个
	 * */
	function setSignCache(one){
		var resList = getSignCache();
		var list = resList.list,  //数据数组
			idstring = resList.idstr; //历史ID字符串集合
		if (list.length==0) {
			idstring = one.signId;
		} else {
			idstring = idstring+','+one.signId;
		}
		list[list.length] = one;
		var signNew = {
			// 数据数组
			list: list,
			// 历史ID字符串集合
			idstr: idstring,
		};
		cacheSet(signList, signNew);
	}
</script>